
function isFunction(obj) {
  return typeof obj === 'function';
}

//弹窗
function showModal(titleTxt, contentTxt, showCancelVal, successCb) {
  wx.showModal({
    title: titleTxt,
    content: contentTxt,
    showCancel: showCancelVal,
    cancelText: "取消",
    confirmText: "确定",
    confirmColor: "#4797ca",
    success: function (res) {
      isFunction(successCb) && successCb(res);
    }
  })
}

// 获取当前时间
function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "-";
  var seperator2 = ":";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  var minutes = date.getMinutes();
  var hours = date.getHours();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  if (hours >= 0 && hours <= 9) {
    hours = "0" + hours;
  }
  if (minutes >= 0 && minutes <= 9) {
    minutes = "0" + minutes;
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    + " " + hours + seperator2 + minutes;
  return currentdate;
}

//登录
function wxLogin(func) {
  // console.log(this)
  //调用登录接口
  //1.小程序调用wx.login得到code.
  var _this = this;
  wx.login({
    success: function (res) {
      var code = res['code'];
      //   console.log(code)
      //2.小程序调用wx.getUserInfo得到rawData, signatrue, encryptData.
      wx.getUserInfo({
        success: function (info) {
          // console.log(info);
          var rawData = info['rawData'];
          var signature = info['signature'];
          var encryptData = info['encryptData'];
          var encryptedData = info['encryptedData']; //注意是encryptedData不是encryptData...坑啊
          var iv = info['iv'];

          //3.小程序调用server获取token接口, 传入code, rawData, signature, encryptData.
          wx.request({
            url: 'https://im.17link.cc/PsyFace/login',
            data: {
              "code": code,
              "rawData": rawData,
              "signature": signature,
              "encryptData": encryptData,
              'iv': iv,
              'encryptedData': encryptedData
            },
            success: function (res) {
              // console.log(res)
              if (res.statusCode != 200) {
                wx.showModal({
                  title: '登录失败'
                });
              } else {
                var session = res.data.session;
                var msg = {};
                msg.nick_name = res.data.nick_name;
                msg.avatar_url = res.data.avatar_url;
                wx.setStorage({    //session3rd存入微信缓存
                  key: "3rd_session",
                  data: session,
                  success: function () {
                    console.log('session3rd存入缓存')
                  }
                })
                wx.setStorage({    //session3rd存入微信缓存
                  key: "msg",
                  data: msg,
                  success: function () {
                    // console.log('个人信息存入缓存')
                  }
                })
              }
              typeof func == "function" && func(res.data);
            }
          });
        }
      });
    }
  });
}

module.exports = {
  isFunction,
  showModal,
  getNowFormatDate,
  wxLogin
}
